package com.example.rule;


import com.example.rule.action.RateAction;
import com.example.rule.condition.CommonCondition;
import com.example.rule.core.Condition;
import com.example.rule.core.Facts;
import com.example.rule.core.Rules;
import com.example.rule.enums.SelectorEnum;
import com.example.rule.fact.CommonFact;
import com.example.rule.rule.CommonRule;

import java.math.BigDecimal;
import java.time.ZonedDateTime;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        //规则，系统启动即初始化入缓存 模拟数据库三条数据
        Condition condition = new CommonCondition();
        condition.addExpression("times > 3");
        condition.addExpression("times <= 4");
        condition.setSelector(SelectorEnum.AND);


       //接口的传值后实例化为某一种事实
        CommonFact todayFact = new CommonFact("today");
        todayFact.setMoney(new BigDecimal("30000"));
        todayFact.setTimes(4);
        todayFact.setTotal(new BigDecimal("350000"));



        Facts facts = new Facts();
        facts.addFact(todayFact);


        //执行动作,总数 * 0.01 费率
        RateAction action = new RateAction("#total * 0.0001");
        CommonRule rule = new CommonRule("one day rule","一天的规则",condition,action);
        rule.setPriority(1);

        rule.setCreateTime(ZonedDateTime.now());



        Rules rules = new Rules(rule);
        //初始化引擎
        RuleEngine engine  = new RuleEngine();
        //如果通过就跳过后续的，设置此选项最大值不生效
        engine.setSkipIfOnePass(true);
        //发现异常直接终止
        engine.setIfExceptionThrow(true);
        //取最大值
        engine.setTakeMax(false);
        //执行操作
        BigDecimal result = engine.start(facts,rules);
        System.out.println(result);
    }
}


